#!/bin/bash

#type -P curl &>/dev/null || { echo "Please install curl first." >&2; exit 1; } type -P uuidgen &>/dev/null || { echo "Please uuidgen curl first." >&2; exit 1; }

SERVER=http://localhost:8080/buzzwordbingo/rest/
GAMES=games/
PLAYERS=players/
BOARDS=boards/
CURRENT_USER=~/.buzzwordbingo_current_user
CURRENT_PASS=~/.buzzwordbingo_current_pass

USER=$1
if [ $1 == "-u" ]; then
	shift; AUTH_USER=$1; shift
fi

if [ $1 == "-p" ]; then
	shift; AUTH_PASS=$1; shift
fi

if [ "$AUTH_USER" == "" -a -e $CURRENT_USER ]; then
	AUTH_USER=`cat $CURRENT_USER`;
fi
if [ "$AUTH_PASS" == "" -a -e $CURRENT_PASS ]; then
	AUTH_PASS=`cat $CURRENT_PASS`;
fi

if [ $1 == "get" ]; then
	curl -X GET -D - $SERVER$2
elif [ $1 == "login" ]; then
	echo -n "Name: "
	read NAME
	PASS=`uuidgen`
	echo "Password: $PASS" 
	cat | curl -X POST -D - -d @- $SERVER$PLAYERS <<- __END__
	<player>
		<name>$NAME</name>
		<password>$PASS</password>
	</player>
	__END__
	echo $NAME > $CURRENT_USER
	echo $PASS > $CURRENT_PASS
elif [ $1 == "logout" ]; then
	NAME=$2
	curl -u $AUTH_USER:$AUTH_PASS -X DELETE -D - $SERVER$PLAYERS$NAME
	rm ~/.buzzwordbingo_current_user
	rm ~/.buzzwordbingo_current_pass
elif [ $1 == "join" ]; then
	echo -n "Game: "
	read GAME
	cat | curl -u $AUTH_USER:$AUTH_PASS -X POST -D - -d @- "$SERVER$GAMES$GAME/$PLAYERS" <<- __END__
	<player>
		<name>$AUTH_USER</name>
	</player>
	__END__
elif [ $1 == "leave" ]; then
	echo -n "Game: "
	read GAME
	echo "$SERVER$GAMES$GAME/$PLAYERS$AUTH_USER/"
	curl -u $AUTH_USER:$AUTH_PASS -X DELETE -D - "$SERVER$GAMES$GAME/$PLAYERS$AUTH_USER/"
elif [ $1 == "change" ]; then
	ID=$2
	cat | curl -u $AUTH_USER:$AUTH_PASS -X PUT -D - -d @- "$SERVER$BOARDS$ID" <<- __END__
	<board>
		<buzzwords>
			<buzzword>
			  <row>0</row>
			  <col>0</col>
			  <text>a</text>
			  <marked>true</marked>
			</buzzword>
			<buzzword>
			  <row>0</row>
			  <col>1</col>
			  <text>b</text>
			  <marked>true</marked>
			</buzzword>
			<buzzword>
			  <row>0</row>
			  <col>2</col>
			  <text>c</text>
			  <marked>true</marked>
			</buzzword>
			<buzzword>
			  <row>1</row>
			  <col>0</col>
			  <text>d</text>
			  <marked>false</marked>
			</buzzword>
			<buzzword>
			  <row>1</row>
			  <col>1</col>
			  <text>e</text>
			  <marked>true</marked>
			</buzzword>
			<buzzword>
			  <row>1</row>
			  <col>2</col>
			  <text>f</text>
			  <marked>false</marked>
			</buzzword>
			<buzzword>
			  <row>2</row>
			  <col>0</col>
			  <text>g</text>
			  <marked>false</marked>
			</buzzword>
			<buzzword>
			  <row>2</row>
			  <col>1</col>
			  <text>h</text>
			  <marked>true</marked>
			</buzzword>
			<buzzword>
			  <row>2</row>
			  <col>2</col>
			  <text>i</text>
			  <marked>false</marked>
			</buzzword>
		</buzzwords>
	</board>	
	__END__
elif [ $1 == "create" ]; then
	echo -n "Title: "
	read TITLE
	echo -n "Size: "
	read SIZE
	echo -n "Description: "
	read DESC
	i=1
	if [ $((SIZE%2)) == 0 ]; then
		count=$((SIZE*SIZE))
	else
		count=$((SIZE*SIZE-1))
	fi
	while [ $i -le $count ]; do
		echo -n "Buzzword $i:"
		read WORD
		BUZZWORDS="$BUZZWORDS<buzzword>$WORD</buzzword>\n"
		i=$((i+1))
	done
	cat | curl -u $AUTH_USER:$AUTH_PASS -X POST -D - -d @- $SERVER$GAMES <<- __END__
	<game>
		<title>$TITLE</title>
		<size>$SIZE</size>
		<description>$FOOBAR</description>
		<buzzwords>$BUZZWORDS</buzzwords>
	</game>
	__END__
fi
